package io.gitee.zhangbinhub.admin.oauth.entity

import com.fasterxml.jackson.annotation.JsonIgnore
import com.mybatisflex.annotation.Id
import com.mybatisflex.annotation.RelationManyToMany
import com.mybatisflex.annotation.Table
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty
import org.apache.commons.lang3.builder.EqualsBuilder
import org.apache.commons.lang3.builder.HashCodeBuilder
import org.noear.snack4.annotation.ONodeAttr

/**
 * @since JDK 11
 */
@Table("t_role")
@ApiModel("角色信息")
data class Role(
    @Id
    @ApiModelProperty(name = "角色ID")
    var id: String = "",
    @ApiModelProperty(name = "应用ID")
    var appId: String = "",
    @ApiModelProperty(name = "角色名称")
    var name: String = "",
    @ApiModelProperty(name = "角色编码")
    var code: String = "",
    @ApiModelProperty(name = "角色级别")
    var levels: Int = 0,
    @ApiModelProperty(name = "序号")
    var sort: Int = 0,
    @ApiModelProperty(name = "类型")
    var type: Int? = null,
    @JsonIgnore
    @field:ONodeAttr(ignore = true)
    @RelationManyToMany(
        joinTable = "t_user_role_set", // 中间表
        joinSelfColumn = "role_id",
        joinTargetColumn = "user_id"
    )
    var userSet: MutableList<User> = mutableListOf(),
    @JsonIgnore
    @field:ONodeAttr(ignore = true)
    @RelationManyToMany(
        joinTable = "t_role_menu_set", // 中间表
        joinSelfColumn = "role_id",
        joinTargetColumn = "menu_id"
    )
    var menuSet: MutableList<Menu> = mutableListOf(),
    @JsonIgnore
    @field:ONodeAttr(ignore = true)
    @RelationManyToMany(
        joinTable = "t_role_module_func_set", // 中间表
        joinSelfColumn = "role_id",
        joinTargetColumn = "module_id"
    )
    var moduleFuncSet: MutableList<ModuleFunc> = mutableListOf()
) {
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || javaClass != other.javaClass) return false
        val role = other as Role
        return EqualsBuilder()
            .appendSuper(super.equals(other))
            .append(id, role.id)
            .append(appId, role.appId)
            .append(name, role.name)
            .append(code, role.code)
            .append(levels, role.levels)
            .append(sort, role.sort)
            .append(type, role.type)
            .isEquals
    }

    override fun hashCode(): Int =
        HashCodeBuilder(17, 37)
            .appendSuper(super.hashCode())
            .append(id)
            .append(appId)
            .append(name)
            .append(code)
            .append(levels)
            .append(sort)
            .append(type)
            .toHashCode()

    override fun toString(): String =
        StringBuilder("Role(")
            .append("id=$id")
            .append(",appId=$appId")
            .append(",name=$name")
            .append(",code=$code")
            .append(",levels=$levels")
            .append(",sort=$sort")
            .append(",type=$type")
            .append(")")
            .toString()
}
